package com.organization

class DataService {

    def commonService
    def departmentService
    def projectService
    def employeeService

    //get all fields for organization node
    def getOrganizationInfo(){
        def result = [:]
        result.putAll(commonService.goodResponse)
        result.putAt("name",commonService.rootNodeStr)
        return result
    }

    //get all element fields by nodeId
    def getElement(nodeId) {
		if (nodeId==commonService.rootNodeStr)
		{
			return getOrganizationInfo()
		}
		else if (nodeId=~commonService.depNodeStr)
		{
			return departmentService.getDepartmentInfo(nodeId)
		}
		else if (nodeId=~commonService.projNodeStr)
		{
			return projectService.getProjectInfo(nodeId)
		}
		else if (nodeId=~commonService.emplNodeStr)
		{
			return employeeService.getEmployeeInfo(nodeId)
		}
		return commonService.badResponse;
    }

    //update fields in node by nodeId
    def updateElement(params){
        if (params.nodeId=~commonService.depNodeStr)
        {
            return departmentService.updateDepartmentInfo(params)
        }
        else if (params.nodeId=~commonService.projNodeStr)
        {
            return projectService.updateProjectInfo(params)
        }
        else if (params.nodeId=~commonService.emplNodeStr)
        {
            return employeeService.updateEmployeeInfo(params)
        }
        return commonService.badResponse;
    }

    //add new node to tree by parentNodeId
    def addElement(params){
        if (params.parentNodeId==commonService.rootNodeStr)
        {
            return departmentService.addDepartment(params)
        }
        else if (params.parentNodeId=~commonService.depNodeStr)
        {
            return projectService.addProject(params)
        }
        else if (params.parentNodeId=~commonService.projNodeStr)
        {
            return employeeService.addEmployee(params)
        }
        return commonService.badResponse;
    }

    //delete node from tree by nodeId
    def deleteElement(params){
        if (params.nodeId=~commonService.depNodeStr)
        {
            return departmentService.deleteDepartment(params)
        }
        else if (params.nodeId=~commonService.projNodeStr)
        {
            return projectService.deleteProject(params)
        }
        else if (params.nodeId=~commonService.emplNodeStr)
        {
            return employeeService.deleteEmployee(params)
        }
        return commonService.badResponse;
    }

}
